package com.kezesong.leasing.service.impl;

import com.kezesong.leasing.common.IdGeneratorUtil;
import com.kezesong.leasing.controller.request.AddOrderReqBody;
import com.kezesong.leasing.controller.response.GoodsDetailsRspBody;
import com.kezesong.leasing.entity.GoodsColorEntity;
import com.kezesong.leasing.entity.GoodsInfoEntity;
import com.kezesong.leasing.entity.GoodsRAMEntity;
import com.kezesong.leasing.entity.OrderEntity;
import com.kezesong.leasing.mapper.OrderMapper;
import com.kezesong.leasing.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${machineId}")
    private String machineId = "0";

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private GoodsInfoService goodsInfoService;

    @Autowired
    private GoodsColorService goodsColorService;

    @Autowired
    private GoodsRamService goodsRamService;

    @Autowired
    private WeixinService weixinService;

    @Override
    public List<OrderEntity> getOrderList(int pageSize, int pageIndex) {
        Example example = new Example(GoodsInfoEntity.class);
        example.createCriteria().andEqualTo("status", 0);

        List<OrderEntity> orderEntities = orderMapper.selectByExample(example);
        return orderEntities;
    }

    @Override
    public OrderEntity getOrder(String orderNo) {
        return getOrderInfo(orderNo);
    }

    @Override
    public String addOrder(OrderEntity orderEntity) {
        String orderNo = IdGeneratorUtil.generatorIdStr(machineId);
        orderEntity.setOrderNo(orderNo);
        GoodsDetailsRspBody goodsDetails = goodsInfoService.getGoodsDetails(orderEntity.getGoodId());
        GoodsColorEntity goodsColor = goodsColorService.getGoodsColorList(orderEntity.getGoodId())
                .stream().filter(goodsColorEntity -> goodsColorEntity.getGoodsId() == orderEntity.getGoodId())
                .findFirst().orElse(new GoodsColorEntity());
        GoodsRAMEntity goodsRAM = goodsRamService.getGoodsRAMList(orderEntity.getGoodId()).stream()
                .filter(p -> p.getGoodsId() == orderEntity.getGoodId())
                .findFirst().orElse(new GoodsRAMEntity());

        BigDecimal totalPrice = goodsDetails.getPrice().add(goodsColor.getDiscountPrice())
                .add(goodsRAM.getDiscountPrice());

        orderEntity.setTotalPrice(totalPrice);

        String goodsName = goodsDetails.getGoodsName()+ " "+ goodsColor.getColorName() + " "+ goodsRAM.getRamName();
        orderEntity.setGoodsName(goodsName);
        int rows = orderMapper.insert(orderEntity);
        if(rows>0){
            return orderNo;
        }
        return null;
    }


    @Override
    public int addOrder(AddOrderReqBody addOrderReqBody) {
        int colorId = addOrderReqBody.getColorId();
        int ramId = addOrderReqBody.getRamId();
        String orderNo = IdGeneratorUtil.generatorIdStr(machineId);
        OrderEntity orderEntity = new OrderEntity();
        BeanUtils.copyProperties(addOrderReqBody, orderEntity);
        orderEntity.setOrderNo(orderNo);

        GoodsDetailsRspBody goodsDetails = goodsInfoService.getGoodsDetails(orderEntity.getGoodId());
        GoodsColorEntity goodsColor = goodsColorService.getGoodsColorList(orderEntity.getGoodId())
                .stream().filter(goodsColorEntity -> goodsColorEntity.getGoodsId() == orderEntity.getGoodId()
                        && goodsColorEntity.getColorId() == colorId)
                .findFirst().orElse(new GoodsColorEntity());
        GoodsRAMEntity goodsRAM = goodsRamService.getGoodsRAMList(orderEntity.getGoodId()).stream()
                .filter(p -> p.getGoodsId() == orderEntity.getGoodId() && p.getRamId() == ramId)
                .findFirst().orElse(new GoodsRAMEntity());

        BigDecimal totalPrice = goodsDetails.getPrice().add(goodsColor.getDiscountPrice())
                .add(goodsRAM.getDiscountPrice());

        orderEntity.setTotalPrice(totalPrice);

        String goodsName = goodsDetails.getGoodsName()+ " "+ goodsColor.getColorName() + " "+ goodsRAM.getRamName();
        orderEntity.setGoodsName(goodsName);
        return 0;
    }

    @Override
    public int modifyOrder(OrderEntity orderEntity) {
        int rows = orderMapper.updateByPrimaryKey(orderEntity);
        return rows;
    }



    @Override
    public int modifyOrderTraceStatus(int orderId, int traceStatus) {
        logger.info("OrderServiceImpl.modifyOrderTraceStatus orderId:{}, traceStatus:{}", orderId, traceStatus);
        OrderEntity orderEntity = orderMapper.selectByPrimaryKey(orderId);
        orderEntity.setTradeStatus(traceStatus);
        orderEntity.setUpdatedTime(new Date());
        int rows = orderMapper.updateByPrimaryKey(orderEntity);
        logger.info("OrderServiceImpl.modifyOrderTraceStatus rows:{}, ", rows);
        return rows;
    }

    @Override
    public String getPrepayNumber(String orderNo, String openId) {
        OrderEntity orderEntity = this.getOrderInfo(orderNo);
        if (null != orderEntity){
            BigDecimal fen = new BigDecimal(100);
            BigDecimal totalPrice = orderEntity.getTotalPrice();
            BigDecimal  totalPriceFen= totalPrice.multiply(fen);

            String description = orderEntity.getGoodsName();
            String prepayNumber = weixinService.createPrepayNumber(openId, orderNo, totalPriceFen, description);
            return prepayNumber;
        }
        return null;
    }


    private OrderEntity getOrderInfo(String orderNo) {
        Example example = new Example(OrderEntity.class);
        example.createCriteria().andEqualTo("orderNo", orderNo);
        OrderEntity orderEntity = orderMapper.selectOneByExample(example);
        return orderEntity;
    }
}
